<template>
  <div>
    <!-- 权限点数据展示 -->
    <el-tree
      ref="refTree"
      :data="list"
      :props="{ label: 'name' }"
      :show-checkbox="true"
      default-expand-all
      :check-strictly="true"
      node-key="id"
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="hCancel()">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { arrayToTree } from "@/utils/index";
import { getPermissionList } from "@/api/permisson";
import { getRoleDetail,assignPerm } from "@/api/setting";
export default {
  props: {
    roleId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      list: [],
    };
  },
  created() {
    this.loadPermission();
    this.loadRoleDetail();
  },
  methods: {
    async hSubmit() {
      const permIds = this.$refs.refTree.getCheckedKeys();
      // console.log('当前选中的节点数组是', permIds)
      const res = await assignPerm({
        id: this.roleId,
        permIds,
      });
      console.log("保存角色的权限点的结果是", res);

      // 提示
      this.$message.success("保存角色的权限成功");
      // 关闭弹层
      this.hCancel();
    },
    async loadRoleDetail() {
      const res = await getRoleDetail(this.roleId);
      // 回填
      this.$refs.refTree.setCheckedKeys(res.data.permIds);
    },
    async loadPermission() {
      const { data } = await getPermissionList();
      this.list = arrayToTree(data);
    },
    hCancel() {
      // 通过父组件去关闭弹层
      this.$emit("close");
    },
  },
};
</script>

<style>
</style>